package panels;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.Border;

import main.Game;
import main.Interface;


public class ProgressPanel extends JPanel implements ActionListener, MouseListener {

	private Interface context;
	private Color col;

	private static final long serialVersionUID = 1L;
	
	private JProgressBar bar;
	private JButton showbutton;
	private JButton deletebutton;
	private JLabel gameName;
	private JLabel filename;
	
	private final Game game;
	
	private long maxSize;
	private long doneSize;
	private long curFileMax;
	
	public ProgressPanel(Interface context, Game g){
		this.context = context;
		this.col = this.getBackground();
		this.setMaximumSize(new Dimension(1000,60));
		this.game = g;
		
		this.addMouseListener(this);
		
		Border blackline = BorderFactory.createEtchedBorder();
		this.setBorder(blackline);
		
	    GridBagLayout gbl = new GridBagLayout();
	    this.setLayout( gbl );
	    
	    bar = new JProgressBar();
	    bar.setStringPainted(true);
	    bar.setPreferredSize(new Dimension(25,20));
	    addComponent( this, gbl, bar, 
	    		GridBagConstraints.HORIZONTAL, 
	    		0, 0, 2, 1, 1.0, 0 );
	    
	    showbutton = new JButton(new ImageIcon(getClass().getResource("/small/info.png")));
	    showbutton.setPreferredSize(new Dimension(40,20));
	    showbutton.setActionCommand("show");
	    showbutton.addMouseListener(this);
	    showbutton.addActionListener(this);
	    addComponent( this, gbl, showbutton, 
	    		GridBagConstraints.NONE, 
	    		2, 0, 1, 1, 0, 0 );
	    
	    deletebutton = new JButton(new ImageIcon(getClass().getResource("/small/delete.png")));
	    deletebutton.setPreferredSize(new Dimension(40,20));
	    deletebutton.setActionCommand("delete");
	    deletebutton.addMouseListener(this);
	    deletebutton.addActionListener(this);
	    addComponent( this, gbl, deletebutton, 
	    		GridBagConstraints.NONE, 
	    		3, 0, 1, 1, 0, 0 );
	    
	    gameName = new JLabel(game.getGame());
	    gameName.setFont(new Font(gameName.getFont().getFontName(), Font.BOLD, gameName.getFont().getSize()));
	    
	    addComponent( this, gbl, gameName, 
	    		GridBagConstraints.HORIZONTAL, 
	    		0, 1, 4, 1, 1.0, 0 );
	    
	    filename = new JLabel(game.getAbsPath());
	    addComponent( this, gbl, filename, 
	    		GridBagConstraints.HORIZONTAL, 
	    		0, 2, 4, 1, 1.0, 0 );
	}

	private void addComponent( Container cont,
			GridBagLayout gbl,
			Component c,
			int fill,
			int x, int y,
			int width, int height,
			double weightx, double weighty ){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = fill;
		gbc.gridx = x; gbc.gridy = y;
		gbc.gridwidth = width; gbc.gridheight = height;
		gbc.weightx = weightx; gbc.weighty = weighty;
		gbl.setConstraints( c, gbc );
		cont.add( c );
	}

	@Override
	public void actionPerformed(ActionEvent ev) {
		String command = ev.getActionCommand();
		if(command.equalsIgnoreCase("delete")){
			context.removeGameFromSync(game.getGame());
		}else if(command.equalsIgnoreCase("show")){
			JOptionPane.showMessageDialog(context.getFrame(), "Show");
		}
	}

	@Override
	public String toString() {
		return this.game.getGame();
	}
	
	public synchronized void setMaxSize(long size){
		this.maxSize = size;
		doneSize = 0;
	}
	
	public long getMaxSize(){
		return maxSize;
	}
	
	public synchronized void setNewFile(String path, long maxSize){
		curFileMax = maxSize;
		filename.setText(path+" 0/"+curFileMax);
		int barvalue = Math.round(1.0f*doneSize/this.maxSize*100);
		bar.setValue(barvalue);
		this.repaint();
		
	}
	
	public synchronized void setFileProgress(String path, long progress){
		filename.setText(path+" "+progress+"/"+curFileMax);
		int barvalue = Math.round((1.0f*doneSize+progress)/maxSize*100);
		bar.setValue(barvalue);
		this.repaint();
	}
	
	public synchronized void setGameDone(){
		bar.setValue(100);
		filename.setText("---DONE---");
		this.repaint();
	}

	public synchronized void addDoneFile(long size) {
		doneSize += size;	
	}

	@Override
	public void mouseClicked(MouseEvent e) {}

	@Override
	public void mouseEntered(MouseEvent e) {
		this.setBackground(Color.LIGHT_GRAY);	
	}

	@Override
	public void mouseExited(MouseEvent e) {
		this.setBackground(col);
	}

	@Override
	public void mousePressed(MouseEvent e) {}

	@Override
	public void mouseReleased(MouseEvent e) {}

}
